<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主键id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入主键id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动状态" prop="hdzt">
        <el-input
          v-model="queryParams.hdzt"
          placeholder="请输入活动状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kjgl:kjsp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kjgl:kjsp:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kjgl:kjsp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        v-hasPermi="['kjgl:kjsp:export']"
      >导出</el-button>
    </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="kjspList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="砍价图片" align="center" prop="kjtp" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.kjtp" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="砍价名称" align="center" prop="jkmc" />
      <el-table-column label="砍价价格" align="center" prop="kjjg" />
      <el-table-column label="最低价" align="center" prop="zdj" />
      <el-table-column label="参与人数" align="center" prop="cyrs" />
      <el-table-column label="帮忙砍价人数" align="center" prop="bmkjrs" />
      <el-table-column label="砍价成功人数" align="center" prop="kjcgrs" />
      <el-table-column label="限量" align="center" prop="xl" />
      <el-table-column label="限量剩余" align="center" prop="xlsy" />
      <el-table-column label="活动状态" align="center" prop="hdzt" >
        <template slot-scope="scope">
          {{scope.row.hdzt==0?'已结束':'进行中'}}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="jssj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jssj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="sjzt" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.sjzt"
            active-color="#13ce66"
            active-text="正常"
            inactive-text="未开启"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kjgl:kjsp:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="cope(scope.row)"
            v-hasPermi="['kjgl:kjsp:edit']"
          >复制</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kjgl:kjsp:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-loading"
            @click="handleCountProduct(scope.row)"
          >统计</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="CountDialog" width="1200px" append-to-body>
      <template>
        <el-table
          :data="count">
          <el-table-column  width="50">
            <template slot-scope="scope">
              <input type="radio"  v-model="form.ggid" :value="scope.row.id"  @click="fgid(scope.row.id)"></input>
            </template>
          </el-table-column>
          <el-table-column label="主键id" align="center" prop="id" />
          <el-table-column label="砍价图片" align="center" prop="kjtp" >
            <template slot-scope="scope">
              <image-preview :src="scope.row.kjtp" :width="50" :height="50"/>
            </template>
          </el-table-column>
          <el-table-column label="砍价名称" align="center" prop="jkmc" />
          <el-table-column label="砍价价格" align="center" prop="kjjg" />
          <el-table-column label="最低价" align="center" prop="zdj" />
          <el-table-column label="参与人数" align="center" prop="cyrs" />
          <el-table-column label="帮忙砍价人数" align="center" prop="bmkjrs" />
          <el-table-column label="砍价成功人数" align="center" prop="kjcgrs" />
          <el-table-column label="限量" align="center" prop="xl" />
          <el-table-column label="限量剩余" align="center" prop="xlsy" />
          <el-table-column label="活动状态" align="center" prop="hdzt" >
            <template slot-scope="scope">
              {{scope.row.hdzt==0?'已结束':'进行中'}}
            </template>
          </el-table-column>
          <el-table-column label="结束时间" align="center" prop="jssj" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.jssj, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="上架状态" align="center" prop="sjzt" >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.sjzt"
                active-color="#13ce66"
                active-text="正常"
                inactive-text="未开启"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-dialog>
    <!-- 添加或修改砍价商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-steps :active="active" finish-status="success">
        <el-step title="选择砍价商品"></el-step>
        <el-step title="填写基础信息"></el-step>
        <el-step title="修改商品详情"></el-step>
        <el-step title="修改商品规则"></el-step>
      </el-steps>
        <div v-show="active==0||active==1" v-if="title=='添加砍价商品'">
          <el-form-item label="选择商品">
            <div @click="handleSpxx()">
              <image-upload style="pointer-events: none" v-model="imageShow"></image-upload>
            </div>
          </el-form-item>
        </div>
        <el-dialog
          append-to-body
          title="提示"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose">
          <template>
            <el-table
              :data="tableData"
              stripe
              style="width: 100%"
              @selection-change="handleSelectionProduct">
              <el-table-column
                type="selection"
                width="55">
              </el-table-column>
              <el-table-column
                prop="id"
                label="商品id"
                width="180">
              </el-table-column>
              <el-table-column
                prop="image"
                label="商品图片"
                width="180">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.image" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="productName"
                label="商品信息">
              </el-table-column>
            </el-table>
          </template>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="setFrom">确 定</el-button>
        </span>
        </el-dialog>
        <div v-if="active==1">
          <el-form-item label="砍价图片" prop="kjtp">
            <image-upload v-model="form.kjtp"/>
          </el-form-item>
          <el-form-item label="砍价活动名称" prop="jkmc">
            <el-input v-model="form.jkmc" placeholder="请输入砍价活动名称" />
          </el-form-item>
          <el-form-item label="砍价活动简介" prop="kjhdjj">
            <el-input v-model="form.kjhdjj" placeholder="请输入砍价活动简介" />
          </el-form-item>
          <el-form-item label="活动开始时间" prop="hdkssj">
            <el-date-picker clearable
                            v-model="form.hdkssj"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择活动开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="jssj">
            <el-date-picker clearable
                            v-model="form.jssj"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择结束时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="物流方式" prop="wlfs">
            <el-checkbox-group v-model="wlfs">
              <el-checkbox label="快递" ></el-checkbox>
              <el-checkbox label="到店核销" ></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="运费设置" prop="yfsz">
            <el-radio-group v-model="form.yfsz">
            <el-radio  :label="1" >固定邮费</el-radio>
            <el-radio  :label="2">运费模板</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="固定邮费" prop="gdyf" v-if="form.yfsz==1">
            <el-input v-model="form.gdyf" placeholder="请输入固定邮费" />
          </el-form-item>
          <el-form-item label="" prop="yfmb" v-if="form.yfsz==2">
          <el-select v-model="form.yfmb" placeholder="请选择">
            <el-option
              v-for="item in productListSelectName"
              :key="item.id"
              :label="item.productName"
              :value="item.id">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="砍价价格" prop="kjjg">
            <el-input v-model="form.kjjg" placeholder="请输入砍价价格" />
          </el-form-item>
          <el-form-item label="最低价" prop="zdj" v-if="active==5">
            <el-input v-model="form.zdj" placeholder="请输入最低价" />
          </el-form-item>
          <el-form-item label="参与人数" prop="cyrs">
            <el-input v-model="form.cyrs" placeholder="请输入参与人数" />
          </el-form-item>
          <el-form-item label="帮忙砍价人数" prop="bmkjrs">
            <el-input v-model="form.bmkjrs" placeholder="请输入帮忙砍价人数" />
          </el-form-item>
          <el-form-item label="砍价成功人数" prop="kjcgrs">
            <el-input v-model="form.kjcgrs" placeholder="请输入砍价成功人数" />
          </el-form-item>
          <el-form-item label="限量" prop="xl" v-if="active==5">
            <el-input v-model="form.xl" placeholder="请输入限量" />
          </el-form-item>
          <el-form-item label="限量剩余" prop="xlsy">
            <el-input v-model="form.xlsy" placeholder="请输入限量剩余" />
          </el-form-item>
          <el-form-item label="砍价人数" prop="kjrs">
            <el-input v-model="form.kjrs" placeholder="请输入砍价人数" />
          </el-form-item>
          <el-form-item label="帮砍次数" prop="bkcs">
            <el-input v-model="form.bkcs" placeholder="请输入帮砍次数" />
          </el-form-item>
          <el-form-item label="购买数量限制" prop="gmslxz">
            <el-input v-model="form.gmslxz" placeholder="请输入购买数量限制" />
          </el-form-item>
          <el-form-item label="最低价" prop="zdj">
            <el-input v-model="form.zdj"  />
          </el-form-item>
          <el-form-item label="砍价起始金额" prop="kjqsje">
            <el-input v-model="form.kjqsje"  />
          </el-form-item>
          <el-form-item label="限量" prop="xl">
            <el-input v-model="form.xl"  />
          </el-form-item>
          <el-form-item label="单位" prop="dw">
            <el-input v-model="form.dw" placeholder="请输入单位" />
          </el-form-item>
          <el-form-item label="排序" prop="px">
            <el-input v-model="form.px" placeholder="请输入排序" />
          </el-form-item>
          <el-form-item label="砍价是否参与分销" prop="kjsfcyfx">
            <el-switch
              style="display: block"
              v-model="form.kjsfcyfx"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              active-text="开启"
              inactive-text="关闭">
            </el-switch>
          </el-form-item>
          <el-form-item label="活动状态" prop="sjzt">
            <el-switch
              style="display: block"
              v-model="form.sjzt"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              active-text="开启"
              inactive-text="关闭">
            </el-switch>
          </el-form-item>
          <el-form-item label="规格选择" prop="ggid">
          <template v-if="this.active==1">
            <el-table :data="tableDataGg" stripe
                      style="width: 100%"
                      @selection-change="handleSelectionProductGg">
              <el-table-column
                type="selection"
                width="55">
                <template slot-scope="scope">
                  <input type="radio"  v-model="form.ggid" :value="scope.row.id"  @click="fgid(scope.row)"></input>
                </template>
              </el-table-column>
              <el-table-column label="尺码" align="center" prop="cm" />
              <el-table-column label="颜色分类" align="center" prop="color" />
              <el-table-column label="图片" align="center" prop="tp" >
                <template slot-scope="scope">
                  <image-preview :src="scope.row.tp" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column label="砍价起始金额" align="center" prop="dhjf" >
                <template slot-scope="scope" >
                  <el-input  v-model="scope.row.dhjf" placeholder="请输入砍价起始金额" />
                </template>
              </el-table-column>
              <el-table-column label="砍价最低价" align="center" prop="zdj" >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.zdj" placeholder="请输入砍价起始金额" />
                </template>
              </el-table-column>
              <el-table-column label="库存" align="center" prop="kc" />
              <el-table-column label="限量" align="center" prop="xl" >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.xl" placeholder="请输入限量" />
                </template>
              </el-table-column>
              <el-table-column label="重量" align="center" prop="zl" />
              <el-table-column label="体积" align="center" prop="tj" />
            </el-table>
          </template>
          </el-form-item>
        </div>
        <div v-if="active==2">
          <el-form-item label="详情">
            <editor v-model="form.xq" :min-height="192"/>
          </el-form-item>
        </div>
        <div v-if="active==3">
          <el-form-item label="规格">
            <editor v-model="form.ge" :min-height="192"/>
          </el-form-item>
        </div>
      </el-form>
      <el-button style="margin-top: 12px;" @click="shang">上一步</el-button>
      <el-button style="margin-top: 12px;" @click="next">下一步</el-button>
      <div slot="footer" class="dialog-footer" v-if="active==3">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKjsp, getKjsp, delKjsp, addKjsp, updateKjsp } from "@/api/kjgl/kjsp";
import { listIntegral, getIntegral, delIntegral, addIntegral, updateIntegral ,ggList} from "@/api/integral/integral";
import { listComments, getComments, delComments, addComments, updateComments,productList } from "@/api/comments/comments";
import {CHECKED} from "@riophae/vue-treeselect/src/constants";
export default {
  name: "Kjsp",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      CountDialog:false,
      active: null,
      // 非多个禁用
      multiple: true,
      dialogVisible:false,
      imageShow:null,
      tableDataGg:[],
      tableData:[],
      // 显示搜索条件
      showSearch: true,
      multipleSelection:[],
      productListSelectName:[],
      hobby:[],
      count:[],
      // 总条数
      total: 0,
      wlfs:[],
      // 砍价商品表格数据
      kjspList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      daterangeCreateTime: [],
      listGg:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        hdzt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.selectGg();
    this.getSelectProduct();
  },
  methods: {
    fgid(row){
      this.listGg[0] = row
      this.form.ggid=row.id;
      this.form.xl=row.xl;
      this.form.zdj=row.zdj;
      this.form.kjqsje=row.dhjf;
    },
    handleCountProduct(row){
      this.$router.push({ path: "/ptgl/tj", query: { id: row.id} })
    },
    handleCount(){
      this.count=[];
      this.CountDialog=false
    },
    tiaozhan(){
      this.$router.push({ path: "/coupons" });
    },
    CHECKED() {
      return CHECKED
    },
    setGg(val){
      this.formGg=val;
    },
    selectGg(){
      ggList(null).then(res=>{
        this.tableDataGg=res;
      })
    },
    handleSpxx(){
      this.dialogVisible=true;
    },
    handleSelectionProductGg(val){
      this.listGg = val
      try {
        // 确保 this.listGg 是数组且不为空
        if (Array.isArray(this.listGg) && this.listGg.length > 0) {
          console.log(this.listGg[0]); // 输出数组中的第一个元素
        } else {
          console.log("this.listGg is undefined or empty.");
        }
      } catch (error) {
        console.error("Error accessing properties:", error);
      }
      this.form.zdj=this.listGg[0].zdj;
      this.form.kjqsje=this.listGg[0].dhjf;
      this.form.xl=this.listGg[0].xl;
      console.log(val)
    },
    setFrom(){
      this.form.kjtp=this.multipleSelection[0].image;
      this.form.jkmc=this.multipleSelection[0].productName;
      this.form.sptp=this.multipleSelection[0].image;
      this.form.dw=this.multipleSelection[0].unit;
      this.form.px=this.multipleSelection[0].sort;
      this.imageShow=this.multipleSelection[0].image;
      this.form.xq='<img src="'+this.multipleSelection[0].image+'">';
      this.dialogVisible=false;
    },
    removeBackslashes(str) {
      // 使用 replace 方法去除所有的反斜杠
      return str.replace(/<[^>]*>/g, '');
    },
    getSelectProduct(){
      productList().then(res=>{
        this.tableData=res;
        this.productListSelectName=res;
      })
    },
    handleSelectionProduct(val){
      this.multipleSelection = val
      if (val.length > 1) {
        this.$refs.multipleTableFrag.clearSelection()
        this.$refs.multipleTableFrag.toggleRowSelection(val.pop())
      }
      console.log(val[0]['name'])
    },
    handleClose(){
      this.$confirm('确认关闭？')
        .then(_ => {
          this.dialogVisible=false
        })
        .catch(_ => {});
    },
    shang(){
      if (this.active-- > 0) this.active = this.active--;
    },
    next() {
      if (++this.active > 3) this.active = 0;
    },
    /** 查询砍价商品列表 */
    getList() {
      this.loading = true;
      listKjsp(this.queryParams).then(response => {
        this.kjspList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.active=0;
      this.wlfs=[];
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        kjtp: null,
        jkmc: null,
        kjjg: null,
        zdj: null,
        cyrs: null,
        bmkjrs: null,
        kjcgrs: null,
        xl: null,
        xlsy: null,
        hdzt: null,
        jssj: null,
        sjzt: null,
        sptp: null,
        kjhdmc: null,
        kjhdjj: null,
        hdkssj: null,
        wlfs: [],
        yfsz: null,
        gdyf: null,
        yfmb: null,
        kjrs: null,
        bkcs: null,
        gmslxz: null,
        dw: null,
        px: null,
        kjsfcyfx: null,
        kjqsje: null,
        xq: null,
        ge: null,
        ggid:null
      };
      this.wlfs=[];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.imageShow=null;
      this.active=0;
      this.reset();
      this.open = true;
      this.title = "添加砍价商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.active=1;
      this.reset();
      const id = row.id || this.ids
      getKjsp(id).then(response => {
        this.form = response.data;
        this.form.ggid=response.data.ggid;
        this.wlfs=this.form.wlfs;
        this.form.xq='<img src="'+this.form.sptp+'">';
        this.form.ge='<img src="'+this.form.sptp+'">';
        this.open = true;
        this.title = "修改砍价商品";
      });
    },
    cope(row){
      this.active=1;
      this.reset();
      const id = row.id || this.ids
      getKjsp(id).then(response => {
        this.form = response.data;
        this.form.id=null;
        this.open = true;
        this.title = "复制砍价商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.wlfs=this.wlfs;
            this.form.ge=this.form.sptp
            this.form.xq=this.form.sptp
            updateKjsp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.wlfs=this.wlfs;
            if (this.form.xq!=null&&this.form.xq!=''){
              this.form.xq=this.removeBackslashes(this.form.xq)
            }
            addKjsp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除砍价商品编号为"' + ids + '"的数据项？').then(function() {
        return delKjsp(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kjgl/kjsp/export', {
        ...this.queryParams
      }, `kjsp_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
